package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcFollowDataTemperatureMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcFollowDataBoxygen;
import com.csun.cmny.provider.model.domain.DmcFollowDataTemperature;
import com.csun.cmny.provider.model.dto.follow.FollowDataPageQuery;
import com.csun.cmny.provider.model.dto.follow.UpFollowDataTemperatureDto;
import com.csun.cmny.provider.model.enums.follow.FollowBoxygenStatusEnum;
import com.csun.cmny.provider.model.enums.follow.FollowTemperatureStatusEnum;
import com.csun.cmny.provider.model.vo.TemperatureForOldmanIds;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcFollowDataTemperatureService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-16 21:49
 * @Description: the class dmc follow data temperature service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcFollowDataTemperatureServiceImpl extends BaseService<DmcFollowDataTemperature>
        implements DmcFollowDataTemperatureService {

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcFollowDataTemperatureMapper followDataTemperatureDao;

    /**
     * query dmc follow data temperature list with page
     *
     * @param followDataPageQuery follow data query page
     *
     * @return the page info
     */
    @Override
    public PageInfo queryDmcFollowDataTemperatureListWithPage(FollowDataPageQuery followDataPageQuery) {

        PageHelper.startPage(followDataPageQuery.getPageNum(), followDataPageQuery.getPageSize());

        List<DmcFollowDataTemperature> dmcFollowDataTemperatureList = followDataTemperatureDao
                .queryDmcFollowDataTemperatureListWithPage(followDataPageQuery);

        return new PageInfo<>(dmcFollowDataTemperatureList);
    }

    /**
     * up follow data temperature
     *
     * @param upFollowDataTemperatureDto up follow data temperature dto
     * @param loginAuthDto               login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer upFollowDataTemperature(UpFollowDataTemperatureDto upFollowDataTemperatureDto,
                                           LoginAuthDto loginAuthDto) {

        // 1.校验设备
        DmcDevice dmcDevice = dmcDeviceService
                .checkDeviceIsFollowByDeviceId(upFollowDataTemperatureDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610041, upFollowDataTemperatureDto.getDeviceId());
        }

        // 2.校验参数
        Preconditions.checkArgument(!PubUtils.isNull(upFollowDataTemperatureDto.getTemperature()),
                cmnyMessageSource.getMessage("follow.up.temperature.null", null));

        // 3.换算状态
        String status = FollowTemperatureStatusEnum.getStatus(upFollowDataTemperatureDto.getTemperature());
        if (PubUtils.isNull(status)) {
            status = cmnyMessageSource.getMessage("follow.temperature.status.error", null);
        }

        // 4.插入数据
        DmcFollowDataTemperature dmcFollowDataTemperature = new DmcFollowDataTemperature();
        BeanUtils.copyProperties(upFollowDataTemperatureDto, dmcFollowDataTemperature);
        dmcFollowDataTemperature.setStatus(status);
        dmcFollowDataTemperature.setUpdateInfo(loginAuthDto);

        return followDataTemperatureDao.insertSelective(dmcFollowDataTemperature);
    }

    @Override
    public Map<String, Object> getRecentData(Integer oldmanId) {
        return followDataTemperatureDao.getRecentData(oldmanId);
    }

    @Override
    public PageInfo queryDmcFollowDataTemperatureListWithPages(FollowDataPageQuery followDataPageQuery) {
        PageHelper.startPage(followDataPageQuery.getPageNum(), followDataPageQuery.getPageSize());

        List<TemperatureForOldmanIds> dmcFollowDataTemperatureList = followDataTemperatureDao
                .queryDmcFollowDataTemperatureListWithPages(followDataPageQuery);

        return new PageInfo<>(dmcFollowDataTemperatureList);
    }
}
